// pages/address/user-address/user-address.js
var app = getApp()
Page({
  data: {
    address: [],
    radioindex: '',
    pro_id:0,
    num:0,
  },
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    //********订单选择地址
    var select = options.select;
    if(select=='true'){
      that.setData({
        select:"yes",
      })
    }
    //********订单选择地址end
    var pro_id = options.pro_id;
    var num = options.num;
    wx.request({
      url: app.d.myUrl + '/Api/Address/index',
      data: {
        user_id: app.globalData.userInfo.id,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      
      success: function (res) {
        // success
        var address = res.data.adds;
        if (address == '') {
          var address = []
        }
        that.setData({
          address: address,
          pro_id:pro_id,
          num:num
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
    
  },
  radioChange: function (e) {
    console.log(e)
    var that = this
    var login = wx.getStorageSync('login')
    console.log(that.data.address[e.detail.value])
    wx.setStorageSync('radioindex', e.detail.value)
    wx.request({
      url: `${app.globalData.API}/address`,
      data: that.data.address[e.detail.value],
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
    this.onLoad()
  },
  onReady: function () {
    // 页面渲染完成
  },
  setDefault: function(e) {
    var that = this;
    var addrId = e.currentTarget.dataset.id;
    wx.request({
      url: app.d.myUrl + '/Api/Address/set_default',
      data: {
        uid: app.globalData.userInfo.id,
        addr_id:addrId
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      
      success: function (res) {
        // success
        var status = res.data.status;
        if(status==1){
          wx.showToast({
            title: '操作成功！',
            duration: 2000
          });
          
          that.DataonLoad();
        }else{
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  delAddress: function (e) {
    var that = this;
    var addrId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '你确认移除吗',
      success: function(res) {
        res.confirm && wx.request({
          url: app.d.myUrl + '/Api/Address/del_adds',
          data: {
            user_id: app.globalData.userInfo.id,
            id_arr:addrId
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {// 设置请求的 header
            'Content-Type':  'application/x-www-form-urlencoded'
          },
          
          success: function (res) {
            // success
            var status = res.data.status;
            if(status==1){
              that.DataonLoad();
            }else{
              wx.showToast({
                title: res.data.err,
                duration: 2000
              });
            }
          },
          fail: function () {
            // fail
            wx.showToast({
              title: '网络异常！',
              duration: 2000
            });
          }
        });
      }
    });

  },
  DataonLoad: function () {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    wx.request({
      url: app.d.myUrl + '/Api/Address/index',
      data: {
        user_id: app.globalData.userInfo.id,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {// 设置请求的 header
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      
      success: function (res) {
        // success
        var address = res.data.adds;
        if (address == '') {
          var address = []
        }
        that.setData({
          address: address,
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
    
  },
  selectme: function(e){
    var that =this;
    if(that.data.select=='yes'){
      var id=e.currentTarget.dataset.addressid;//ID
      var name=e.currentTarget.dataset.addressname;//收货人
      var tel=e.currentTarget.dataset.addressphone;//电话
      var address_xq=e.currentTarget.dataset.addressxq;//地址详情
      // wx.showToast({
      //   title:name,
      // })
      var pages = getCurrentPages();//获得当前页面
      var prevPage = pages[pages.length - 2];  //获得上一个页面
      prevPage.setData({
        address: {id:id, name:name, tel:tel, address_xq:address_xq}
      })
      wx.navigateBack();
    }
  },
})